package com.cnap.multiclouldmgmt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.RepByPage;
import com.cnap.multiclouldmgmt.model.enums.K8sStatusEnum;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterModel;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.service.PrometheusService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

class PrometheusServiceImplTest extends MulticlouldmgmtApplicationTests {

    @MockBean
    private MultiCloudMgmtService multiCloudMgmtService;

    @Autowired
    private PrometheusService prometheusService;

    @Test
    void getPrometheusRangeData() throws Exception {

        //"yyyy-MM-dd'T'HH:mm:ss"
        String beginDate = "2022-11-22T11:11:11";
        String endeDate = "2022-11-22T11:31:11";
        Map<String,Object> repose = new HashMap<>();
        repose.put("status","success");
        Map<String,Object> result = new HashMap<>();
        List<Object> returnData = new ArrayList<>();
        Map<String,Object> returnNo1 = new HashMap<>();
        returnData.add(returnNo1);
        Map<String,String> metricMap = new HashMap<>();
        metricMap.put("job","job");
        metricMap.put("instance","instance");
        returnNo1.put("metric",metricMap);

        result.put("result",returnData);
        repose.put("data",result);

        String k8sClusterIp = CnapCloudMgmtConstants.TEST_ADMIN + JSONObject.toJSONString(repose);
        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModel.setK8sClusterIp(k8sClusterIp);
        k8sClusterModel.setStatus(K8sStatusEnum.Running.getChineseDescribe());
        RepByPage<K8sClusterModel> k8sClusterModels = new RepByPage<>();
        k8sClusterModels.setResult(new ArrayList<>());
        k8sClusterModels.getResult().add(k8sClusterModel);
        Mockito.when(multiCloudMgmtService.getK8sClusterNum(null,null,CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(1);
        Mockito.when(multiCloudMgmtService.getK8sClusterInfos(null,null,1,1,
                CnapCloudMgmtConstants.TEST_ADMIN)).thenReturn(k8sClusterModels);

        prometheusService.getPrometheusRangeData("psql",CnapCloudMgmtConstants.TEST_ADMIN,
                beginDate,endeDate);

    }

    @Test
    void getNodeDiskThroughputByMbPerS() {

        Map<String,Object> repose = new HashMap<>();
        repose.put("status","success");
        Map<String,Object> result = new HashMap<>();
        List<Object> returnData = new ArrayList<>();
        Map<String,Object> returnNo1 = new HashMap<>();
        returnData.add(returnNo1);
        List<Double> vaules = new ArrayList<>();
        vaules.add(1.2);
        returnNo1.put("value",vaules);

        result.put("result",returnData);
        repose.put("data",result);

        String k8sClusterIp = CnapCloudMgmtConstants.TEST_ADMIN + JSONObject.toJSONString(repose);
        prometheusService.getNodeDiskThroughputByMbPerS(k8sClusterIp,"1.1",null);
    }

    @Test
    void getNodeDiskIOPS() {

        Map<String,Object> repose = new HashMap<>();
        repose.put("status","success");
        Map<String,Object> result = new HashMap<>();
        List<Object> returnData = new ArrayList<>();
        Map<String,Object> returnNo1 = new HashMap<>();
        returnData.add(returnNo1);
        List<Double> vaules = new ArrayList<>();
        vaules.add(1.2);
        returnNo1.put("value",vaules);

        result.put("result",returnData);
        repose.put("data",result);

        String k8sClusterIp = CnapCloudMgmtConstants.TEST_ADMIN + JSONObject.toJSONString(repose);
        prometheusService.getNodeDiskIOPS(k8sClusterIp,"1.1",null);
    }

    @Test
    void getNodeNetBandwidthBybps() {
        Map<String,Object> repose = new HashMap<>();
        repose.put("status","success");
        Map<String,Object> result = new HashMap<>();
        List<Object> returnData = new ArrayList<>();
        Map<String,Object> returnNo1 = new HashMap<>();
        returnData.add(returnNo1);
        List<Double> vaules = new ArrayList<>();
        vaules.add(1.2);
        returnNo1.put("value",vaules);

        result.put("result",returnData);
        repose.put("data",result);

        String k8sClusterIp = CnapCloudMgmtConstants.TEST_ADMIN + JSONObject.toJSONString(repose);
        prometheusService.getNodeNetBandwidthBybps(k8sClusterIp,"1.1",null);
    }

    @Test
    void getNodeNetIOPS() {
        Map<String,Object> repose = new HashMap<>();
        repose.put("status","success");
        Map<String,Object> result = new HashMap<>();
        List<Object> returnData = new ArrayList<>();
        Map<String,Object> returnNo1 = new HashMap<>();
        returnData.add(returnNo1);
        List<Double> vaules = new ArrayList<>();
        vaules.add(1.2);
        returnNo1.put("value",vaules);

        result.put("result",returnData);
        repose.put("data",result);

        String k8sClusterIp = CnapCloudMgmtConstants.TEST_ADMIN + JSONObject.toJSONString(repose);
        prometheusService.getNodeNetIOPS(k8sClusterIp,"1.1",null);
    }

    @Test
    void getNodeDiskFreeUnitB() {
        Map<String,Object> repose = new HashMap<>();
        repose.put("status","success");
        Map<String,Object> result = new HashMap<>();
        List<Object> returnData = new ArrayList<>();
        Map<String,Object> returnNo1 = new HashMap<>();
        returnData.add(returnNo1);
        List<Double> vaules = new ArrayList<>();
        vaules.add(1.2);
        returnNo1.put("value",vaules);

        result.put("result",returnData);
        repose.put("data",result);

        String k8sClusterIp = CnapCloudMgmtConstants.TEST_ADMIN + JSONObject.toJSONString(repose);
        prometheusService.getNodeDiskFreeUnitB(k8sClusterIp,"1.1",null);
    }
}